﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace RESP_IIWaterAssessment
{
    public partial class PlantProtectionKindForm : Form
    {
        public PlantProtectionKindForm()
        {
            InitializeComponent();
        }

        tb_plantprotectionkind PlantProtect;
        bool insert = false;
        const string catMeasure = "Dimension";
        int dimID = 0;

        private void PlantProtectionKindForm_Load(object sender, EventArgs e)
        {
            rbBio.Checked = true;

            cbDimension.SelectedIndexChanged -= cbDimension_SelectedIndexChanged;
            cbDimension.DataSource = UtilClass.FillCBCategory(catMeasure);
            cbDimension.DisplayMember = "Name";
            cbDimension.ValueMember = "ID";
            cbDimension.SelectedIndex = -1;
            cbDimension.Text = string.Empty;
            cbDimension.SelectedIndexChanged += new EventHandler(cbDimension_SelectedIndexChanged);
        }

        private void cbDimension_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbDimension.SelectedIndex >= 0)
                dimID = Convert.ToInt32(cbDimension.SelectedValue);
            else
                dimID = 0;
        }

        private void rbBio_CheckedChanged(object sender, EventArgs e)
        {
            ShowPlantProtection(rbAIDS.Checked);
        }

        private void ShowPlantProtection(bool isAIDS)
        {
            dgvPlantProtection.DataSource = DBClass.PlantProtection.Where(wh => wh.ProtectionType == isAIDS).ToList();

            dgvPlantProtection.Columns["ID"].Visible = false;
            dgvPlantProtection.Columns["ProtectionType"].Visible = false;
            dgvPlantProtection.Columns["Name"].HeaderText = "Русское название";
            dgvPlantProtection.Columns["NameUz"].HeaderText = "Узбекское название";
            dgvPlantProtection.Columns["Measure"].HeaderText = "Размерность";
            dgvPlantProtection.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvPlantProtection.AutoResizeColumns();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            tbLossName.Enabled = tbLossNameUzb.Enabled = cbDimension.Enabled = insert = true;
            buttonOK.Text = "Добавить";
            buttonCancel.Text = "No";
            PlantProtect = null;
        }

        private void tsbEdit_Click(object sender, EventArgs e)
        {
            if (dgvPlantProtection.RowCount > 0 && dgvPlantProtection.CurrentRow != null)
            {
                insert = false;
                tbLossName.Enabled = tbLossNameUzb.Enabled = cbDimension.Enabled = true;
                int lID = (int)dgvPlantProtection["ID", dgvPlantProtection.CurrentRow.Index].Value;
                PlantProtect = DBClass.PlantProtection.Where(wh => wh.ID == lID).SingleOrDefault();
                if (PlantProtect != null)
                {
                    tbLossName.Text = PlantProtect.Name;
                    tbLossNameUzb.Text = PlantProtect.NameUz;
                }
            }
        }

        private void tsbDelete_Click(object sender, EventArgs e)
        {
            if (dgvPlantProtection.RowCount > 0 && dgvPlantProtection.CurrentRow != null)
            {
                int lID = (int)dgvPlantProtection["ID", dgvPlantProtection.CurrentRow.Index].Value;
                PlantProtect = DBClass.PlantProtection.Where(wh => wh.ID == lID).SingleOrDefault();

                string cName = InformationClass.Lang ? PlantProtect.Name : PlantProtect.NameUz;
                if (MessageBox.Show("Вы действительно хотите удалить вид затрат \"" + cName + " \"", "Внимание!", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    DBClass.PlantProtection.DeleteOnSubmit(PlantProtect);
                    DBClass.RESPContext.SubmitChanges();
                    ShowPlantProtection(rbBio.Checked);
                }
                else
                {
                    PlantProtect = null;
                }

            }
        }

        private void dgvPlantProtection_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if (e.ColumnIndex == dgvPlantProtection.Columns.IndexOf(dgvPlantProtection.Columns["Measure"]))
            {
                if (e.Value == null)
                    return;
                else
                {
                    if ((int)e.Value == 0)
                        e.Value = "NO DATA";
                    else
                        e.Value = UtilClass.GetCategoryDisplayName((int)e.Value, catMeasure);
                }
            }
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (insert && PlantProtect == null)
            {
                if (String.IsNullOrEmpty(tbLossName.Text) || String.IsNullOrEmpty(tbLossNameUzb.Text) || dimID == 0)
                {
                    if (InformationClass.Lang)
                        MessageBox.Show("Обязательно заполнить оба названия видов защиты и выбрать измерение!");
                    else
                        MessageBox.Show("Ҳимоя турларининг иккита номини тўлдириш ва ўлчамни танлаш шарт!");
                    return;
                }

                PlantProtect = new tb_plantprotectionkind
                {
                    Name = tbLossName.Text,
                    NameUz = tbLossNameUzb.Text,
                    ProtectionType = rbAIDS.Checked,
                    Measure = dimID
                };
                DBClass.PlantProtection.InsertOnSubmit(PlantProtect);

                tbLossName.Text = tbLossNameUzb.Text = string.Empty;


            }
            else
            {
                if (PlantProtect != null)
                {
                    PlantProtect.Name = tbLossName.Text;
                    PlantProtect.NameUz = tbLossNameUzb.Text;
                    PlantProtect.Measure = dimID;
                    PlantProtect.ProtectionType = rbAIDS.Checked;
                }

                this.Close();
            }

            try
            {
                if (DBClass.RESPContext.GetChangeSet().Deletes.Count > 0 || DBClass.RESPContext.GetChangeSet().Inserts.Count > 0 || DBClass.RESPContext.GetChangeSet().Updates.Count > 0)
                {
                    DBClass.RESPContext.SubmitChanges();
                    MessageBox.Show(msgClass.msgSubmitChanges, msgClass.msgAchtung);
                }
                ShowPlantProtection(rbBio.Checked);
                tbLossName.Enabled = tbLossNameUzb.Enabled = cbDimension.Enabled = insert = false;
                buttonOK.Text = "OK";
                buttonCancel.Text = "Cancel";
            }
            catch
            {
                MessageBox.Show(msgClass.msgError, msgClass.msgAchtung);
            }
        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

    }
}
